package org.share.comm.os;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 操作系统相关工具类
 * @author hhm
 *
 */
public class OSUtils {
	
	public static String getOSName(){
		return System.getProperty("os.name");
	}

	public static int getCPUCount(){
		return Runtime.getRuntime().availableProcessors();
	}
	
	public static long getJVMFreeMemory(){
		return Runtime.getRuntime().freeMemory();
	}
	
	public static long getJVMTotalMemory(){
		return Runtime.getRuntime().totalMemory();
	}
	
	public static long getJVMMaxMemory(){
		return Runtime.getRuntime().maxMemory();
	}
	
	/**
	 * 获取当前机器的ip地址
	 * @return
	 */
	public static String getLocalIP(){
		String ip = null;
		InetAddress localAddr;
		try {
			localAddr = Inet4Address.getLocalHost();
			ip = localAddr.getHostAddress();
		} catch (UnknownHostException e) {
//			_logger.error("获取当前机器的ip地址错误！", e);
			e.printStackTrace();
		}
		return ip;
	}

	
	public static void main(String[] args) {
		System.out.println(OSUtils.getLocalIP());
		System.out.println(Runtime.getRuntime().totalMemory());
		System.out.println(Runtime.getRuntime().freeMemory());
		System.out.println(Runtime.getRuntime().maxMemory());
	}
	
}
